package com.sgb.goods.widget.pop;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.goods.R;
import com.sgb.goods.entity.order.OrderPayDetailEntity;
import com.sgb.goods.entity.order.OrderPayInfoEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.widget.MaxHeightRecyclerView;
import com.sgb.goods.widget.pop.adapter.PopPayInfoDetailAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.DisplayUtil;
import com.swgk.core.util.StringUtil;

/**
 * 订单详情弹框
 *
 * @author cyj
 */
public class OrderPayInfoPop extends BasePopWindow<OrderPayDetailEntity> {

    TextView business_name, order_num, type;
    private String orderType;
    private String orderId;
    private int orderIdentity;
    private int paymentWay;
    private int paymentMethod;
    private int orderProcessState;

    public OrderPayInfoPop(Context context) {
        super(context, DIR_DOWN_UP);
    }

    private PopPayInfoDetailAdapter adapter;

    @Override
    protected int popLayout() {
        return R.layout.goods_pop_pay_info;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        TextView iv_close = holder.getPop().findViewById(R.id.close);
        business_name = holder.getPop().findViewById(R.id.business_name);
        order_num = holder.getPop().findViewById(R.id.order_num);
        type = holder.getPop().findViewById(R.id.type);
        TextView title = holder.getPop().findViewById(R.id.title);
        title.setText("支付信息");
        MaxHeightRecyclerView maxHeightRecyclerView = holder.getPop().findViewById(R.id.recyclerView);
        iv_close.setOnClickListener(v -> this.dismiss());
        adapter = new PopPayInfoDetailAdapter(mContext, null);
        maxHeightRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        maxHeightRecyclerView.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<OrderPayDetailEntity>() {
            @Override
            public void onItemClick(OrderPayDetailEntity entity, int position) {
                startOrderDetails();
            }
        });
    }

    public void showPop(View view, OrderPayInfoEntity entity, String orderType) {
        this.orderId = entity.orderId;
        this.paymentWay = entity.paymentWay;
        this.paymentMethod = entity.paymentMethod;
        this.orderType = orderType;
        this.orderIdentity = entity.orderIdentity;
        this.orderProcessState = entity.orderProcessState;
        adapter.setDatas(entity.pays);
        business_name.setText(entity.companyName);
        order_num.setText(entity.orderId);
        type.setText("1".equals(orderType) ? "商家" : "采购商");
        setHeight(DisplayUtil.dip2px(mContext, 450));
        this.showDownPopwindow(view, true);
    }

    /**
     * 跳转订单详情界面
     */
    private void startOrderDetails() {
        if (StringUtil.isEmpty(orderId)) {
            return;
        }
        ARouter.getInstance().build(Constants.ARouter.AR_PAY_INFORMATION)
                .withString("orderId", orderId)
                .withString("orderType", orderType)
                .withInt("orderProcessState", orderProcessState)
                .withInt("paymentWay", paymentWay)
                .withInt("orderIdentity", orderIdentity)
                .withInt("paymentMethod", paymentMethod)
                .navigation();
        dismiss();
    }

}
